<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class OrderController extends Controller
{
    public function getIndex(Request $request)
    {
        //搜索
        $rows = $request->input('num',5); //获取行数
        $orders = DB::table('shop_orders')->where('shop_orders.user_id',session('id'))->where(function($query) use ($request){ 
            $k = $request->input('keyword'); //获取搜索条件
            if(!empty($k)){
                $query->where('order_num','like','%'.$k.'%');
                
            } 
            // $query->where('shop_orders.user_id','=',session('id'));
         })->join('shop_address','shop_orders.address_id','=','shop_address.id')
        ->join('shop_users','shop_address.user_id','=','shop_users.id')
        ->select('*','shop_orders.id as ord_id','shop_orders.status as ord_sta')->paginate($rows);
        // dd($orders);
        //获取总价
        foreach($orders as $k=>$v){
            $tp = total($v->ord_id);
            // echo $total,'<br>';
            $orders[$k]->tp = $tp; 
        }
        // dd($orders);
        //发货状态
        $status = ['待付款','已付款/未发货','已发货','订单完成','已评价','关闭订单'];
        //获取物流名称数组
        $wlName = expressName();
        // dd(session('id'));
        //跳转订单页面,分配变量
        return view('admin.order.index',[
            'request'=>$request,
            'tp'=>$tp, //总价
            'orders'=>$orders,  //要遍历的顶大信息
            'status'=>$status,  //发货状态
            'wlName'=>$wlName  //快递编码对应名称的数组
            ]);
    }


    //详情信息
    public function getDetail(Request $request)
    {
        //搜索
        $orders = DB::table('shop_orders')->where('shop_orders.id',$request->input('id'))
        ->join('shop_address','shop_orders.address_id','=','shop_address.id')
        ->join('shop_users','shop_address.user_id','=','shop_users.id')
        ->select('*','shop_orders.id as ord_id')->first();
        //商品信息
        // dd($orders);
        $goods = DB::table('shop_orders')->where('shop_orders.id',$request->input('id'))
        ->join('shop_ordgoods','shop_orders.id','=','shop_ordgoods.order_id')->first();

        if($goods->goods_id > 120){
            $goods = DB::table('shop_orders')->where('shop_orders.id',$request->input('id'))
                ->join('shop_ordgoods','shop_orders.id','=','shop_ordgoods.order_id')
                ->join('shop_active','shop_ordgoods.goods_id','=','shop_active.id')
                ->select('*','shop_active.id as gprice','shop_orders.id as pics')->get();
        }else{
            $goods = DB::table('shop_orders')->where('shop_orders.id',$request->input('id'))
                ->join('shop_ordgoods','shop_orders.id','=','shop_ordgoods.order_id')
                ->join('shop_goods','shop_ordgoods.goods_id','=','shop_goods.id')
                ->select('*','shop_goods.id as gprice','shop_orders.id as pics')->get();
        }
        
        // dd($goods);
        //获取图片
        $tp = 0;

        foreach($goods as $k=>$v){
            //商品单价
            $price = $v->price;
            //商品数量
            $num = $v ->num;
            //获取小计
            $gprice = $price * $num;
            //将每件商品的小计压入goods中
            $goods[$k]->gprice = $gprice;
            //获取商品小图
            if($goods[$k]->goods_id > 120){
                $pic = DB::table('shop_activepics')->where('aid',$goods[$k]->goods_id)->first();
            }else{
                $pic = DB::table('shop_goopics')->where('goods_id',$goods[$k]->goods_id)->first();
            }
            
            // dd($pic);
            $pic = str_replace('b','t',$pic->path);
            $goods[$k]->pics = $pic;
            // $goods[$k]->path
            //获取总价
            $tp += $gprice; 
        }
        //获取快递id
        $id = $orders->express_id;
        if(!empty($id)){
            //获取快递信息
            $express = DB::table('shop_express')->where('id',$id)->first();
            //获取物流信息
            $wlInfo = dealResult($express->express_name,$express->express_code);            
        }else{
            $wlInfo = null;
        }
        //获取物流名称数组
        $wlName = expressName();
        //跳转详情页面,分配变量
        return view('admin.order.detail',[
                    'orders'=>$orders,  //订单基本信息
                    'goods'=>$goods,  //订单内的商品
                    'tp'=>$tp,  //总价
                    'wlInfo'=>$wlInfo,  //物流信息
                    'wlName'=>$wlName  //快递编码对应名称的数组
                    ]);
    }

    //发货
    public function postSend(Request $request)
    {
        $data = $request->except('_token','ord_id');
        // dd($request->input('ord_id'));
        //开启事务
        DB::beginTransaction();
        //插入快递表
        $id = DB::table('shop_express')->insertGetId($data);
        // dd($id);
        if(!empty($id)){
            //更新订单表内的快递id
            $res = DB::table('shop_orders')
                ->where('id',1)
                ->update(['express_id'=>$id,'status'=>2]);
        }
        if(!empty($res)){
            DB::commit();
            return 1;
        }else{
            DB::rollBack();
            return 0;
        }

    }
    
    /*==============================前台=================================*/

    //添加订单
    public function add(Request $request)
    {
        $data = $request->except('_token');
        // dd($data);
        //检测  没有goods_id 的项不要
        
        // dd(session('id'));
        $orders = [];
        //插入订单主表  需要信息 订单号 时间 地址id user_id
        $orders['order_num'] = date('YmdHis',time()).rand(100000,999999);
        $orders['time'] = time();
        $orders['user_id'] = session('id');
        // dd($orders);

        //开启事务
        DB::beginTransaction();
        $id = DB::table('shop_orders')->insertGetId($orders);
        // dd($id);

        //插入order_goodsb表  获取的order_id goods_id goods_size  goods_color num(数量)
       // dd($data);
        $goods = [];
        foreach($data['data'] as $k=>$v){
            $tmp = [];
            if(!empty($v['goods_id'])){
                $tmp['order_id'] = $id;
                $tmp['goods_id'] = $v['goods_id'];
                $tmp['goods_size'] = $v['size'];
                $tmp['goods_color'] = $v['color'];
                $tmp['num'] = $v['num'];
                $goods[] = $tmp;
            }
        }
        // dd($goods);
        $res = DB::table('shop_ordgoods')->insert($goods);
        if($res){
            DB::commit();
            return redirect('/home/order/confirm?order_id='.$id);
        }else{
            DB::rollBack();
            return back()->with('aa','宝贝太惹火,请再次提交哦');
        }
    }

    //订单确认
    public function confirm(Request $request)
    {
        // dd(session('id'));
        $order_id = $request->order_id;
        // dd($order_id);
        $addr = DB::table('shop_address')->where('user_id',session('id'))->get();
        // dd($addr);
        return view('home.order.confirm',['addr'=>$addr,'order_id'=>$order_id]);
    }

    //执行订单确认
    public function doconfirm(Request $request)
    {
        //修改订单表
        $addr_pay = $request->except('_token','order_id');
        //发货状态见  36行
        // $addr_pay['status'] = 1; 
        // dd($addr_pay);
        $res = DB::table('shop_orders')->where('id',$request->order_id)->update($addr_pay);
        
        // dd($addr_pay);

        //判断
        if($res){
            $data = DB::table('shop_orders')->where('id',$request->order_id)->first();
            $tp = total($request->order_id);

            // dd('http://pay.xiaohigh.com/api/deal?to=153016537@qq.com&money='.$tp.'&order\_id='.$data->order_num.'&info=NerviousUp订单支付&return\_url=http://www.xiaohigh.com?order\_id='.$data->order_num.'&status=1&money='.$tp);
            return redirect('http://pay.xiaohigh.com/api/deal?to=153016537@qq.com&money='.$tp.'&order_id='.$request->order_id.'&info=NerviousUp订单支付&return_url=http://123.com/payout?order_id='.$request->order_id.'&status=1&money='.$tp);
        }
    }

      // 付款后的页面
    public function payout(Request $request)
    {

            $arr=[];
           //把随即的商品放到 一个数组中 作为推荐商品
            for($i=0;$i<6;$i++){
               $number = rand(1,120);  
               $goods = DB::table('shop_goods')->where('id',$number)->first();
               $arr[] = $goods;

            }
               
               //获取商品的图片
               foreach($arr as $k => $v){          
                   $path = DB::table('shop_goopics')->where('goods_id',$v->id)->select('path')->first();
                   $arr[$k]->path = $path->path;                
                 }   
            
            //修改订单状态
            $res = DB::table('shop_orders')->where('id',$request->order_id)->update(['status'=>1]);   
           return view('home.order.payout',['arr'=>$arr]);
     }

    //前台订单显示
    public function show(Request $request)
    {
        //获取该用户的所有订单
        $orders = DB::table('shop_orders')->where('user_id',session('id'))->orderBy('id','desc')->get();
        // dd($orders);
        //订单中所需要信息 时间√ 订单号√ 商品图片× 标题× 颜色× 尺码× 单价× 数量× 状态√
        foreach($orders as $k=>$v){
            $ord_goods = DB::table('shop_ordgoods')->where('order_id',$v->id)->get();
            // dd($ord_goods);
            foreach($ord_goods as $kk=>$vv){
                if($vv->goods_id > 120){
                    $title = DB::table('shop_active')->where('id',$vv->goods_id)->select('title','price')->first();
                    $path = DB::table('shop_activepics')->where('aid',$vv->goods_id)->select('path')->first();
                }else{
                    $title = DB::table('shop_goods')->where('id',$vv->goods_id)->select('title','price')->first();
                    $path = DB::table('shop_goopics')->where('goods_id',$vv->goods_id)->select('path')->first();
                }
                $ord_goods[$kk]->title = $title->title;
                $ord_goods[$kk]->price = $title->price;
                $ord_goods[$kk]->path = $path->path;
                //查看商品评价状态 如果为1 已评价

                $comsta = DB::table('shop_comments')->where('goods_id',$vv->goods_id)->where('order_id',$v->id)->select('status')->first();

                $ord_goods[$kk]->comsta = $comsta;
            }
            $orders[$k]->info = $ord_goods;
        }
        // dd($orders);
        return view('home.order.index',['orders'=>$orders]);
    }

    
    
    //通过商品id获取图片
    private function getPicById($id)
    {
        $pic  = DB::table('shop_goopics')->where('goods_id',$id)->select('path')->first();
        return $pic;
    }

    //确认收货
    public function receipt(Request $request)
    {
        $order_id = $request->input('orderid');
        // dd($order_id);
        $res = DB::table('shop_orders')->where('id',$order_id)->update(['status'=>3]);
        if($res){
            return back()->with('info','收货成功');
        }else{
            return back()->with('error','收获失败');
        }
    }

    //删除订单
    public function delete(Request $request)
    {
        $ordid = $request->ordid;
        // dd($ordid);
        //只有订单已评价或订单关闭才可以删除
        $sta = DB::table('shop_orders')->where('id',$ordid)->select('status')->first()->status;
        // dd($sta);
        if($sta < 3) return 0;
        $res = DB::table('shop_orders')->where('id',$ordid)->delete();
        if($res){
            return 1;
        }else{
            return 0;
        }
    }

} 
